package com.pshcp.service;

import cn.hutool.core.date.DateUtil;
import com.framework.base.PageList;
import com.framework.tool.CommonTool;
import com.framework.util.SystemUtil;
import com.github.pagehelper.PageHelper;
import com.pshcp.common.enums.CommonStatusEnum;
import com.pshcp.domain.dto.PatientReserveRecordDTO;
import com.pshcp.domain.model.Doctor;
import com.pshcp.domain.model.PatientReserveRecord;
import com.pshcp.mapper.PatientReserveRecordMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 患者预约表 服务实现类
 * </p>
 *
 * @author auto generator
 * @since 2022-11-12
 */
@Service
public class PatientReserveRecordService {

    @Autowired
    private PatientReserveRecordMapper patientReserveRecordMapper;


    public PageList<PatientReserveRecord> list(PatientReserveRecordDTO patientReserveRecordDTO){
        if(patientReserveRecordDTO.getPageNum() != null && patientReserveRecordDTO.getPageSize() != null){
            PageHelper.startPage(patientReserveRecordDTO.getPageNum(), patientReserveRecordDTO.getPageSize());
        }
        Example example = new Example(PatientReserveRecord.class);
        Example.Criteria criteria = example.createCriteria();
        if(patientReserveRecordDTO.getStatus() != null){
            criteria.andEqualTo("status", patientReserveRecordDTO.getStatus());
        }
        if(patientReserveRecordDTO.getDoctorId() != null){
            criteria.andEqualTo("doctorId", patientReserveRecordDTO.getDoctorId());
        }
        if(patientReserveRecordDTO.getPatientUserId() != null){
            criteria.andEqualTo("patientUserId", patientReserveRecordDTO.getPatientUserId());
        }
        if(patientReserveRecordDTO.getSearchStartTime() != null){
            criteria.andGreaterThanOrEqualTo("treatmentDate", DateUtil.formatDate(patientReserveRecordDTO.getSearchStartTime()));
        }
        if(patientReserveRecordDTO.getSearchEndTime() != null){
            criteria.andLessThanOrEqualTo("treatmentDate", DateUtil.formatDate(patientReserveRecordDTO.getSearchEndTime()));
        }
        if(StringUtils.isNoneBlank(patientReserveRecordDTO.getAccessDeptId())){
            criteria.orEqualTo("accessDeptId", patientReserveRecordDTO.getAccessDeptId());
        }
        example.setOrderByClause(" created_time desc");
        List<PatientReserveRecord> list = patientReserveRecordMapper.selectByExample(example);
        return CommonTool.buildPageList(list);
    }

    public List<PatientReserveRecord> select(PatientReserveRecord patientReserveRecord){
        return patientReserveRecordMapper.select(patientReserveRecord);
    }

    public int insert(PatientReserveRecord patientReserveRecord){
        return patientReserveRecordMapper.insert(patientReserveRecord);
    }

    public int update(PatientReserveRecord patientReserveRecord){
        patientReserveRecord.setUpdateTime(new Date());
        return patientReserveRecordMapper.updateByPrimaryKeySelective(patientReserveRecord);
    }

    public PatientReserveRecord findById(Long id){
        return patientReserveRecordMapper.selectByPrimaryKey(id);
    }

    public List<PatientReserveRecord> findByIds(List<Long> ids){
        Example example = new Example(PatientReserveRecord.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", ids);
        return patientReserveRecordMapper.selectByExample(example);
    }

    public int delById(Long id){
        Example example = new Example(PatientReserveRecord.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);

        PatientReserveRecord patientReserveRecord = new PatientReserveRecord();
        patientReserveRecord.setStatus(CommonStatusEnum.DEL.getCode());
        return patientReserveRecordMapper.updateByExampleSelective(patientReserveRecord, example);
    }

    public List<PatientReserveRecordDTO> queryReserveRecordList(Integer handleType, Doctor doctor, String time){
        Map<String, Object> map = new HashMap<>();
        Long doctorId = doctor.getId();
        map.put("handleType", handleType);
        if(doctorId != null && doctor.getSecretaryFlag() == 0){
            map.put("doctorId", doctorId);
        }
        if(!StringUtils.isEmpty(time)){
            map.put("time", time);
        }
        return patientReserveRecordMapper.queryReserveRecordList(map);
    }
}
